<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<body class="list">
<div th:replace="include/head.html::html"></div>
<div class="layui-fluid layui-anim-upbit layui-anim">
    <div class="layui-card">
        <!--查询from 表单-->
        <div class="layui-card-body">
            <form class="layui-form" >
                <div class="layui-form-item">
                    <div class="layui-inline">
                    <label class="layui-form-label">字典名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="parentName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">字典标识</label>
                    <div class="layui-input-inline">
                        <input type="text" name="parentCode" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    </div>
                    </div>
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto"></div>
                </div>
            </form>
        </div>
            <!--<div class="layui-form-item">
                <div class="layui-inline">

                </div>
                <div class="layui-inline">

                </div>
                <div class="layui-inline">
                    &lt;!&ndash;<button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>&ndash;&gt;

                </div>

            </div>-->

        <!--表格中的数据 并不显示-->
        <div class="layui-card-body">
            <!--表格外部上侧的按钮-->
            <div style="padding-bottom: 10px;" class="tool-bar">
<!--                <button class="layui-btn add-btn" data-type="addLayer" shiro:hasPermission="sysDic:add">添加</button>-->
<!--                <button class="layui-btn add-btn" data-type="refresh" shiro:hasPermission="sysDic:updateRedisDic" ><i class="layui-icon layui-icon-refresh-1"></i>更新字典</button>-->
            </div>
            <!--数据表格 -->
            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="switchTpl">
                <input type="checkbox" name="status" value="{{d.oid}}" lay-skin="switch" lay-text="启用|禁用"
                       shiro:hasPermission="sysDic:updateStatus" lay-filter="status" {{ d.status== 1 ? 'checked' : '' }}>
            </script>
            <!--表格每行的 的操作按钮 -->
            <script type="text/html" id="toolbar2">
                <a class="layui-btn layui-btn-xs" lay-href="sysDicChild/list?parentOid={{ d.oid }}&parentCode={{ d.parentCode }}"
                   shiro:hasPermission="sysDicChild:list">
                    <i class="layui-icon layui-icon-edit"></i>配置“{{d.parentName}}“子级</a>
            </script>
            <script type="text/html" id="toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"
                   shiro:hasPermission="sysDic:edit"><i
                        class="layui-icon layui-icon-edit"></i>修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                   shiro:hasPermission="sysDic:delbean"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    var table;
    layui.use(['jquery','table', 'laydate'], function () {
        table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        table.render({
            elem: '#table'
            , id: 'table'
            , skin: 'line'
            , loading: true
            , method: 'post'
            , height: "full-190"
            , url: 'sysDic/page' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'numbers', title: '序号'}
                , {field: 'parentName', title: '字典名'}
                , {field: 'parentCode', title: '字典标识'}
                // , {field: 'status', title: '状态', templet: '#switchTpl'}
                , {
                    title: '权限', align: 'center'
                    , templet: function (d) {
                        if (d.authority === 1) {
                            return "系统权限";
                        } else if (d.authority === 2) {
                            return "可修改权限";
                        } else if (d.authority === 3) {
                            return "可增删改权限";
                        } else {
                            return "权限出错";
                        }
                    }
                }
                , {field: 'createTime', title: '创建时间'}
                , {field: 'remark', title: '备注'}
                , {title: '配置', width: 300, align: 'center', toolbar: '#toolbar2'}
                // , {title: '操作', width: 300, align: 'center', toolbar: '#toolbar'}
            ]]
        });
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {where: field});
        });
        //监听 按钮
        form.on('switch(status)', function(obj){
            $.post("sysDic/updateStatus", {oid:this.value}, function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg);
                    table.reload('table');
                } else {
                    layer.msg(res.msg);
                }
            });
            // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });
        //监听行工具事件
        //注：tool 是工具条事件名，table 是 table 原始容器的属性 lay-filter="对应的值"
        table.on('tool(table)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                editLayer({
                    title: '编辑字典',
                    url: 'sysDic/edit?oid=' + data.oid,
                    area: ['800px', '500px'],
                    back: callback
                });
            }else if(layEvent === 'del'){
                layer.confirm('确定删除此行吗？', function (index) {
                    $.post("sysDic/del", {oid:data.oid}, function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg);
                            table.reload('table');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        });
        layui.$(".add-btn").on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var active = {
            addLayer: function () {
                var add = editLayer({
                    title: '添加字典',
                    url: 'sysDic/edit',
                    area: ['800px', '500px'],
                    back: callback
                });
            },refresh:function () {
                $.post("sysDic/updateRedisDic", {}, function (res) {
                    if (res.code === 0) {
                        layer.msg(res.msg);
                    } else {
                        layer.msg(res.msg);
                    }
                });
            }
        }
    });

    function callback(ifr, index, data) {
        var field = data.field;
        $.post("sysDic/saveOrUpdate", field, function (res) {
            if (res.code === 0) {
                topLayer.msg(res.msg);
                topLayer.close(index);
                table.reload('table');
                topLayer.close(index);
            } else {
                topLayer.msg(res.msg);
            }
        });
    }
</script>
</body>
</html>
